<template>
	<view class="loginBox">

		<!-- #ifdef MP-USER -->
		<uni-nav-bar @clickLeft="goHome" :statusBar="true" :border="false" :background-color="'transparent'" title="登录"
			:leftIcon="'back'"></uni-nav-bar>
		<!-- #endif -->
		<!-- #ifdef MP-COMPANY -->
		<block v-if="accountLogin">
			<uni-nav-bar @clickLeft="accountLogin=false" :leftIcon="'back'" :statusBar="true" :border="false"
				:background-color="'transparent'" title="登录"></uni-nav-bar>
		</block>
		<block v-else>
			<uni-nav-bar :statusBar="true" :border="false" :background-color="'transparent'" title="登录"></uni-nav-bar>
		</block>
		<!-- #endif -->
		<!-- #ifdef MP-ECONOMIC -->
		<block v-if="accountLogin">
			<uni-nav-bar @clickLeft="accountLogin=false" :leftIcon="'back'" :statusBar="true" :border="false"
				:background-color="'#EAFCFF'" title="登录"></uni-nav-bar>
		</block>
		<block v-else>
			<uni-nav-bar :statusBar="true" :border="false" :background-color="'#EAFCFF'" title="登录"></uni-nav-bar>
		</block>
		<!-- #endif -->

		<view class="loginBody" v-if="!accountLogin">

			<view class="flex jst-center">
				<u-image :width="140" :height="140" :borderRaduis="30" :src="xf.getImgUrl(configInfo?.logo)" mode="" class="logo"></u-image>
			</view>
			<view class="t-center font26 mT25"  >{{configInfo?.name}}</view>
			<view class="flex alg-center flex-v jst-center mT25">
				<button v-if="selectTy" type="primary" class="btnLogin" open-type="getPhoneNumber"
					@getphonenumber="getPhoneNumber">
					<view class="marL20 font34">手机号一键授权登录</view>
				</button>
				<button v-else type="primary" class="btnLogin" @click="tyxi">
					<view class="marL20 font34">手机号一键授权登录</view>
				</button>
				
				<view class="zhBtn" @click="accountLogin=true">使用账号登录</view>
				<view class="mT25  u-8bd">
					<u-checkbox @change="checkboxChange" v-model="selectTy">
						<view class="font12">登录代表您同意<text
								@click.stop="xf.onlineUtils.route('pages/userPage/subpackge/my/yhxy/yhxy?type=YHXY')"
								class="u-blue">《用户协议》</text>及<text
								@click.stop="xf.onlineUtils.route('pages/userPage/subpackge/my/yhxy/yhxy?type=YSZC')"
								class="u-blue">《隐私政策》</text>
						</view>
					</u-checkbox>
				</view>
			</view>
		</view>
		<view class="wrapForm pzForm" v-else>
			<view class="bgLine pB30 pL15" @click="accountLogin=false">
				<view class="font24 bold">你好!</view>
			    <view class="font14 mT5 u-9">欢迎登录{{configInfo?.name}}</view>
			
			</view>
			<view class="wrapFormBody pL15 pR15 pT25">
				<u-form :label-width="300" class="xf-wrap-form" ref="dataForm" :rules="rules" :model="formData">

					<u-form-item :required="true" :label-style="{'font-size':'36rpx'}" :label-position="'top'"
						label="用户名" prop="username">
						<u-input placeholder="请填写" v-model="formData.username" />
					</u-form-item>
					<u-form-item :required="true" :label-style="{'font-size':'36rpx'}" :label-position="'top'"
						label="用户密码" prop="password">
						<u-input placeholder="请填写" type="password" v-model="formData.password" />
					</u-form-item>
					<view class="flex jst-between mT20">
						<view>还没账号，<text class="u_theme_color" @click.stop="xf.onlineUtils.route('pages/signIn/signIn')">去注册</text></view>
						<view class="u_theme_color" @click.stop="xf.onlineUtils.route('pages/retrievePassword/retrievePassword')">忘记密码？</view>
					</view>
					<button type="primary" class="btnLogin mT30" @click="handelLogin">
						<view class="marL20 font34">登录</view>
					</button>
					<view class="mT25  u-8bd">
						<u-checkbox @change="checkboxChange" v-model="selectTy">
							<view class="font12">登录代表您同意<text
									@click.stop="xf.onlineUtils.route('pages/userPage/subpackge/my/yhxy/yhxy?type=YHXY')"
									class="u-blue">《用户协议》</text>及<text
									@click.stop="xf.onlineUtils.route('pages/userPage/subpackge/my/yhxy/yhxy?type=YSZC')"
									class="u-blue">《隐私政策》</text>
							</view>
						</u-checkbox>
					</view>
				</u-form>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		licenseFreelogin,
		loginAccount,
		getLogo,
		usergetOpenId
	} from "@/api/common.js"
	import define from '@/utils/define.js'
	import {chatUserInfo} from "@/api/chat.js"
	export default {
		data() {
			return {
				accountLogin: false,
				selectTy: false,
				configInfo:{},
				formData: {
					// username: "17736133394",
					// password: "XunFeng@2023",
					username: "",
					password: "",
					terminalType: "10"
				},
				rules: {

				}
			}
		},
		onShow() {
			this.getLogoOpt()
			// #ifdef H5
			this.accountLogin = true;
			// #endif
			uni.clearStorageSync('token')
			uni.clearStorageSync('userInfo')
			uni.clearStorageSync('authStatus')
		},
		methods: {
			getWechatLogin() { //// 获取微信unionid openId方法
				uni.login({
					provider: 'weixin', // 指定授权方式为微信
					success: (res) => {
						usergetOpenId({
							type: '3', // 获取类型 网站登录订阅 1 推送订阅2 小程序3
							code: res.code
						}).then(async res => {});
						},
						fail: (err) => {

						}
				});
			},
			getLogoOpt(){
				getLogo().then(res=>{
					this.configInfo=res.result;
					// console.log(res)
				})
			},
			//获取聊天当前用户信息
			getChatUserInfo(){
				chatUserInfo().then(res=>{})
			},
			tyxi() {
				this.xf.onlineUtils.toast('请先同意《用户协议》及《隐私协议》')
			},
			goHome() {
				if (this.accountLogin) {
					this.accountLogin = false;
				} else {
					uni.switchTab({
						url: "/pages/userPage/home/index"
					})
				}

			},
			handelLogin() {
				if (!this.formData.username) {
					this.xf.onlineUtils.toast("请输入用户名")
				} else if (!this.formData.password) {
					this.xf.onlineUtils.toast("请输入用户密码")
				} else {
					if (!this.selectTy) {
						this.xf.onlineUtils.toast('请先同意《用户协议》及《隐私协议》')
					} else {
						loginAccount(this.formData).then(res => {
							if (res.success) {
							
								this.userStore.setToken(res.result.token);
								let info = res.result?.xflinggongOddUserInfoVo;
								this.userStore.setAuthStatus(info.authStatus)
								this.userStore.setUserId(info?.id);
								this.userStore.setUserInfo(JSON.stringify(info))
								this.getChatUserInfo()
								this.xf.onlineUtils.toast("登陆成功")
								getApp().$vm.init()
								setTimeout(() => {
									if (define.identification == 'units') {
										//微信-企业端登录
										uni.setStorageSync('userType', 'units')
										if (info.authStatus == 0) {
											if (info.checkStatus === 0 || info.checkStatus === '0' || info
												.checkStatus == 20) {
												uni.navigateTo({
													url: "/pages/common/waitSh/index"
												})
											} else {
												uni.navigateTo({
													url: "/pages/companyPage/companyStart/index"
												})
											}

										} else {
											uni.switchTab({
												url: "/pages/companyPage/home/index"
											})
										}
									} else if (define.identification == "broker") {
										uni.setStorageSync('userType', 'broker')
										if (info.isAuthAgent != 1) {
											if (info.checkStatus === 0 || info.checkStatus === '0' || info
												.checkStatus == 20) {
												uni.navigateTo({
													url: "/pages/common/waitSh/index"
												})
											} else {
												uni.navigateTo({
													url: "/pages/economicPage/subpackge/rzStart/index"
												})
											}

										} else {
											uni.switchTab({
												url: "/pages/economicPage/home/index"
											})
										}
									} else {
										uni.setStorageSync('userType', 'user')
										var pageUrl = uni.getStorageSync('pageUrl')
										if (pageUrl) {
											// 如果为tabbar页面则用reLaunch跳转
											if (['/pages/userPage/home/index', '/pages/userPage/my/index',
													'/pages/userPage/message/index', '/pages/userPage/job/index'
												].includes(pageUrl)) {
												uni.reLaunch({
													url: pageUrl
												})
											} else {
												uni.redirectTo({
													url: pageUrl
												})
											}
											//跳转后，删除url记录避免重复跳转
											uni.removeStorageSync('pageUrl')
										} else {
											// 如果没有默认跳转到首页
											uni.reLaunch({
												url: '/pages/userPage/home/index'
											})
										}
									}
								}, 1000)
							}
						})

					}

				}
			},
			getPhoneNumber(e) {
				//#ifdef MP-WEIXIN
				if (e.detail.errMsg == "getPhoneNumber:ok") {
					licenseFreelogin({
						code: e.detail.code,
						//0是微信  1是支付宝
						weChatOrAliPay: "0",
						terminalType: 10, //登陆终端 默认10是移动端
						izRegister: 1 //是否注册 0 否  1是
					}).then(async res => {
						if (res.success) {
							this.userStore.setToken(res.result.token);
							let info = res.result?.xflinggongOddUserInfoVo;
							this.userStore.setAuthStatus(info.authStatus)
							this.userStore.setUserId(info?.id);
							this.userStore.setUserInfo(JSON.stringify(info))
							this.getChatUserInfo()
							getApp().$vm.init()
							if (define.identification == 'units') {
								//微信-企业端登录
								uni.setStorageSync('userType', 'units')
								if (info.authStatus == 0) {
									if (info.checkStatus === 0 || info.checkStatus === '0' || info
										.checkStatus == 20) {
										uni.navigateTo({
											url: "/pages/common/waitSh/index"
										})
									} else {
										uni.navigateTo({
											url: "/pages/companyPage/companyStart/index"
										})
									}

								} else {
									uni.switchTab({
										url: "/pages/companyPage/home/index"
									})
								}
							} else if (define.identification == "broker") {
								//微信经济人登录
								uni.setStorageSync('userType', 'broker')
								if (info.isAuthAgent!=1) {
									if (info.checkStatus === 0 || info.checkStatus === '0' || info
										.checkStatus == 20) {
										uni.navigateTo({
											url: "/pages/common/waitSh/index"
										})
									} else {
										uni.navigateTo({
											url: "/pages/economicPage/subpackge/rzStart/index"
										})
									}
								} else {
									uni.switchTab({
										url: "/pages/economicPage/home/index"
									})
								}
							} else {
								uni.setStorageSync('userType', 'user')
								var pageUrl = uni.getStorageSync('pageUrl')
								if (pageUrl) {
									// 如果为tabbar页面则用reLaunch跳转
									if (['/pages/userPage/home/index', '/pages/userPage/my/index',
											'/pages/userPage/message/index', '/pages/userPage/job/index'
										].includes(pageUrl)) {
										uni.reLaunch({
											url: pageUrl
										})
									} else {
										uni.redirectTo({
											url: pageUrl
										})
									}
									//跳转后，删除url记录避免重复跳转
									uni.removeStorageSync('pageUrl')
								} else {
									// 如果没有默认跳转到首页
									uni.reLaunch({
										url: '/pages/userPage/home/index'
									})
								}
							}
							this.getWechatLogin()
						}
					});
				}
				// #endif
			}

		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;

		background: linear-gradient(0deg, #F7F8FB, #E1EEFF);
		/* #ifdef MP-ECONOMIC */
		background: linear-gradient(0deg, #F7F8FB, #EBFCFF);
		/* #endif */
	}
</style>
<style scoped lang="scss">
	.wrapForm {

		height: calc(100% - 100rpx);
		background: #fff;

		.wrapFormBody {
			margin-top: -30rpx;
			border-radius: 40rpx 40rpx 0 0;
			background: #fff;
		}


	}


	.loginBox {
		height: 100%;
	}


	.bgLine {
		background-color: #E1EEFF;
		width: 100%;
		/* #ifdef MP-ECONOMIC */
		background-color: #EBFCFF;
		/* #endif */
	}

	.loginBody {
		padding-top: 128rpx;
	}

	.logo {
		width: 140rpx;
		height: 140rpx;
		border-radius: 30rpx;
	}

	.btnLogin {
		width: 670rpx;
		height: 100rpx;
		line-height: 100rpx;
		background: #386AFF;
		/* #ifdef  MP-ECONOMIC */
		background: #00A0C6;
		/* #endif */
		font-size: 32rpx;
		border-radius: 30rpx 30rpx 30rpx 30rpx;
	}

	.zhBtn {
		width: 670rpx;
		height: 100rpx;
		cursor: pointer;
		border-radius: 30rpx 30rpx 30rpx 30rpx;
		border: 2rpx solid #3291F8;
		color: #3291F8;
		/* #ifdef  MP-ECONOMIC */
		border: 2rpx solid #00A0C6;
		color: #00A0C6;
		/* #endif */
		font-size: 32rpx;
		line-height: 100rpx;
		text-align: center;
		margin-top: 30rpx;
	}
</style>